package com.coder4j.auth.controller;

import com.coder4j.enums.AuthResourceTypeEnum;
import com.coder4j.auth.service.AuthResourceService;
import com.coder4j.auth.vo.AuthResourceQuery;
import com.coder4j.auth.vo.AuthResourceVO;
import com.coder4j.auth.vo.MenuVO;
import com.coder4j.auth.vo.UserMenuVO;
import com.coder4j.cache.ButtonCache;
import com.coder4j.cache.RoleCache;
import com.coder4j.config.GlobalConfig;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资源
 * @author keller
 * @date 2024-05-07 18:10:11
 */
@Slf4j
@RestController
@RequestMapping("/authResource")
public class AuthResourceController {
    @Resource
    private AuthResourceService service;

    @Resource
    private HttpServletResponse response;

    @GetMapping("/userMenu")
    @ApiNote(name = "查询用户菜单",createName = "keller",createTime = "2024-5-9 16:00:46")
    public R userMenu(){
        List<UserMenuVO> list =  service.userMenu();
        return R.success(list);
    }

    @PostMapping("/list")
    @ApiNote(name = "查询资源列表",createName = "keller",createTime = "2024-5-9 16:00:46")
    public R list(@RequestBody AuthResourceQuery query){
        query.setType(AuthResourceTypeEnum.Menu.name());
        BasePage<MenuVO> page =  service.list(query);
        return R.success(page);
    }

    @GetMapping("/tree")
    @ApiNote(name = "查询资源树",createName = "keller",createTime = "2024-5-9 16:00:46")
    public R menuTree(){
        return R.success(service.tree());
    }


    @PostMapping("/init")
    @ApiNote(name="初始化菜单和按钮",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R init(@RequestBody List<MenuVO> list){

        service.initMenuAndButton(list);
        return R.success();
    }

    /**
     * 按ID查询资源
     * @param id AuthResource.id
     * @return 资源对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询资源",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R<AuthResourceVO> getById( Long id){

        log.info("====== AuthResourceControllerGetById ====== {}",id);
        return R.success(service.baseGetVOById(id));
    }


    /**
     * 保存资源
     * 有 id 字段，更新；否则新增
     * @param authResource AuthResource对象
     * @return AuthResource对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存资源",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R save(@RequestBody AuthResourceVO authResource){
        log.info("====== AuthResourceControllerSave ====== {}",authResource);

        if(ObjectUtils.hasEmpty(authResource.getParentId(),authResource.getName(),authResource.getType(),authResource.getWebPath())){
            return R.paramError();
        }
        if(authResource.getSort() == null || authResource.getSort() <= 0){
            authResource.setSort(GlobalConfig.DEFAULT_SORT);
        }

        service.baseSaveVO(authResource);

        ButtonCache.initCache();
        RoleCache.initRoleApiCache();
        return R.success();
    }

    /**
     * 删除资源
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id AuthResource.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除资源",createName = "keller",createTime = "2024-05-07 18:10:11")
    public R deleteById( Long id){
        log.info("====== AuthResourceControllerDeleteById ======  {}",id);
        service.baseDeleteById(id);

        ButtonCache.initCache();
        RoleCache.initRoleApiCache();
        return R.success();
    }
}
